package co.com.tye.presentation.backingBeans;

import co.com.tye.exceptions.*;
import co.com.tye.modelo.*;
import co.com.tye.modelo.dto.DocenteProgramaDTO;
import co.com.tye.presentation.businessDelegate.*;
import co.com.tye.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.RowEditEvent;

import java.io.Serializable;

import java.sql.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;


/**
 * @author Zathura Code Generator http://code.google.com/p/zathura/
 * www.zathuracode.org
 *
 */
@ManagedBean
@ViewScoped
public class DocenteProgramaView implements Serializable {
    private static final long serialVersionUID = 1L;
    private InputText txtIddocente_Docente;
    private InputText txtIdprograma_Programa;
    private InputText txtIddocentePrograma;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<DocenteProgramaDTO> data;
    private DocenteProgramaDTO selectedDocentePrograma;
    private DocentePrograma entity;
    private boolean showDialog;
    @ManagedProperty(value = "#{BusinessDelegatorView}")
    private IBusinessDelegatorView businessDelegatorView;

    public DocenteProgramaView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            DocenteProgramaDTO docenteProgramaDTO = (DocenteProgramaDTO) e.getObject();

            if (txtIddocente_Docente == null) {
                txtIddocente_Docente = new InputText();
            }

            txtIddocente_Docente.setValue(docenteProgramaDTO.getIddocente_Docente());

            if (txtIdprograma_Programa == null) {
                txtIdprograma_Programa = new InputText();
            }

            txtIdprograma_Programa.setValue(docenteProgramaDTO.getIdprograma_Programa());

            if (txtIddocentePrograma == null) {
                txtIddocentePrograma = new InputText();
            }

            txtIddocentePrograma.setValue(docenteProgramaDTO.getIddocentePrograma());

            Integer iddocentePrograma = FacesUtils.checkInteger(txtIddocentePrograma);
            entity = businessDelegatorView.getDocentePrograma(iddocentePrograma);

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_new() {
        action_clear();
        selectedDocentePrograma = null;
        setShowDialog(true);

        return "";
    }

    public String action_clear() {
        entity = null;
        selectedDocentePrograma = null;

        if (txtIddocente_Docente != null) {
            txtIddocente_Docente.setValue(null);
            txtIddocente_Docente.setDisabled(true);
        }

        if (txtIdprograma_Programa != null) {
            txtIdprograma_Programa.setValue(null);
            txtIdprograma_Programa.setDisabled(true);
        }

        if (txtIddocentePrograma != null) {
            txtIddocentePrograma.setValue(null);
            txtIddocentePrograma.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        return "";
    }

    public void listener_txtId() {
        try {
            Integer iddocentePrograma = FacesUtils.checkInteger(txtIddocentePrograma);
            entity = (iddocentePrograma != null)
                ? businessDelegatorView.getDocentePrograma(iddocentePrograma)
                : null;
        } catch (Exception e) {
            entity = null;
        }

        if (entity == null) {
            txtIddocente_Docente.setDisabled(false);
            txtIdprograma_Programa.setDisabled(false);
            txtIddocentePrograma.setDisabled(false);
            btnSave.setDisabled(false);
        } else {
            txtIddocente_Docente.setValue(entity.getDocente().getIddocente());
            txtIddocente_Docente.setDisabled(false);
            txtIdprograma_Programa.setValue(entity.getPrograma().getIdprograma());
            txtIdprograma_Programa.setDisabled(false);
            txtIddocentePrograma.setValue(entity.getIddocentePrograma());
            txtIddocentePrograma.setDisabled(true);
            btnSave.setDisabled(false);

            if (btnDelete != null) {
                btnDelete.setDisabled(false);
            }
        }
    }

    public String action_edit(ActionEvent evt) {
        selectedDocentePrograma = (DocenteProgramaDTO) (evt.getComponent()
                                                           .getAttributes()
                                                           .get("selectedDocentePrograma"));
        txtIddocente_Docente.setValue(selectedDocentePrograma.getIddocente_Docente());
        txtIddocente_Docente.setDisabled(false);
        txtIdprograma_Programa.setValue(selectedDocentePrograma.getIdprograma_Programa());
        txtIdprograma_Programa.setDisabled(false);
        txtIddocentePrograma.setValue(selectedDocentePrograma.getIddocentePrograma());
        txtIddocentePrograma.setDisabled(true);
        btnSave.setDisabled(false);
        setShowDialog(true);

        return "";
    }

    public String action_save() {
        try {
            if ((selectedDocentePrograma == null) && (entity == null)) {
                action_create();
            } else {
                action_modify();
            }

            data = null;
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_create() {
        try {
            entity = new DocentePrograma();

            Integer iddocentePrograma = FacesUtils.checkInteger(txtIddocentePrograma);

            entity.setIddocentePrograma(iddocentePrograma);
            entity.setDocente((FacesUtils.checkInteger(txtIddocente_Docente) != null)
                ? businessDelegatorView.getDocente(FacesUtils.checkInteger(
                        txtIddocente_Docente)) : null);
            entity.setPrograma((FacesUtils.checkInteger(txtIdprograma_Programa) != null)
                ? businessDelegatorView.getPrograma(FacesUtils.checkInteger(
                        txtIdprograma_Programa)) : null);
            businessDelegatorView.saveDocentePrograma(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            entity = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            if (entity == null) {
                Integer iddocentePrograma = new Integer(selectedDocentePrograma.getIddocentePrograma());
                entity = businessDelegatorView.getDocentePrograma(iddocentePrograma);
            }

            entity.setDocente((FacesUtils.checkInteger(txtIddocente_Docente) != null)
                ? businessDelegatorView.getDocente(FacesUtils.checkInteger(
                        txtIddocente_Docente)) : null);
            entity.setPrograma((FacesUtils.checkInteger(txtIdprograma_Programa) != null)
                ? businessDelegatorView.getPrograma(FacesUtils.checkInteger(
                        txtIdprograma_Programa)) : null);
            businessDelegatorView.updateDocentePrograma(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_datatable(ActionEvent evt) {
        try {
            selectedDocentePrograma = (DocenteProgramaDTO) (evt.getComponent()
                                                               .getAttributes()
                                                               .get("selectedDocentePrograma"));

            Integer iddocentePrograma = new Integer(selectedDocentePrograma.getIddocentePrograma());
            entity = businessDelegatorView.getDocentePrograma(iddocentePrograma);
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_master() {
        try {
            Integer iddocentePrograma = FacesUtils.checkInteger(txtIddocentePrograma);
            entity = businessDelegatorView.getDocentePrograma(iddocentePrograma);
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public void action_delete() throws Exception {
        try {
            businessDelegatorView.deleteDocentePrograma(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data = null;
        } catch (Exception e) {
            throw e;
        }
    }

    public String action_closeDialog() {
        setShowDialog(false);
        action_clear();

        return "";
    }

    public String actionDeleteDataTableEditable(ActionEvent evt) {
        try {
            selectedDocentePrograma = (DocenteProgramaDTO) (evt.getComponent()
                                                               .getAttributes()
                                                               .get("selectedDocentePrograma"));

            Integer iddocentePrograma = new Integer(selectedDocentePrograma.getIddocentePrograma());
            entity = businessDelegatorView.getDocentePrograma(iddocentePrograma);
            businessDelegatorView.deleteDocentePrograma(entity);
            data.remove(selectedDocentePrograma);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Integer iddocentePrograma,
        Integer iddocente_Docente, Integer idprograma_Programa)
        throws Exception {
        try {
            businessDelegatorView.updateDocentePrograma(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("DocenteProgramaView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtIddocente_Docente() {
        return txtIddocente_Docente;
    }

    public void setTxtIddocente_Docente(InputText txtIddocente_Docente) {
        this.txtIddocente_Docente = txtIddocente_Docente;
    }

    public InputText getTxtIdprograma_Programa() {
        return txtIdprograma_Programa;
    }

    public void setTxtIdprograma_Programa(InputText txtIdprograma_Programa) {
        this.txtIdprograma_Programa = txtIdprograma_Programa;
    }

    public InputText getTxtIddocentePrograma() {
        return txtIddocentePrograma;
    }

    public void setTxtIddocentePrograma(InputText txtIddocentePrograma) {
        this.txtIddocentePrograma = txtIddocentePrograma;
    }

    public List<DocenteProgramaDTO> getData() {
        try {
            if (data == null) {
                data = businessDelegatorView.getDataDocentePrograma();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<DocenteProgramaDTO> docenteProgramaDTO) {
        this.data = docenteProgramaDTO;
    }

    public DocenteProgramaDTO getSelectedDocentePrograma() {
        return selectedDocentePrograma;
    }

    public void setSelectedDocentePrograma(DocenteProgramaDTO docentePrograma) {
        this.selectedDocentePrograma = docentePrograma;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }

    public IBusinessDelegatorView getBusinessDelegatorView() {
        return businessDelegatorView;
    }

    public void setBusinessDelegatorView(
        IBusinessDelegatorView businessDelegatorView) {
        this.businessDelegatorView = businessDelegatorView;
    }

    public boolean isShowDialog() {
        return showDialog;
    }

    public void setShowDialog(boolean showDialog) {
        this.showDialog = showDialog;
    }
}
